<template>
  <Dialog v-model="dialogVisible" :appendToBody="true" title="选择退款活动" width="70%">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        ref="queryFormRef"
        :inline="true"
        :model="queryParams"
        class="-mb-15px"
        label-width="68px"
      >
        <el-form-item label="活动名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入活动名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="活动状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择活动状态"
            clearable
            class="!w-240px"
          >
            <el-option label="开启" :value="0" />
            <el-option label="关闭" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery">
            <Icon class="mr-5px" icon="ep:search" />
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <Icon class="mr-5px" icon="ep:refresh" />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <ContentWrap>
      <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
        <el-table-column type="radio" width="55" v-if="!multiple">
          <template #default="{ row }">
            <el-radio 
              :model-value="selectedActivity?.id" 
              :value="row.id" 
              @change="handleSingleSelected(row)"
            >
              &nbsp;
            </el-radio>
          </template>
        </el-table-column>
        <el-table-column type="selection" width="55" v-if="multiple" />
        <el-table-column label="活动编号" prop="id" width="80" />
        <el-table-column label="活动名称" prop="name" min-width="200" />
        <el-table-column label="开始时间" prop="startTime" width="180">
          <template #default="{ row }">
            {{ formatTimestamp(row.startTime) }}
          </template>
        </el-table-column>
        <el-table-column label="结束时间" prop="endTime" width="180">
          <template #default="{ row }">
            {{ formatTimestamp(row.endTime) }}
          </template>
        </el-table-column>
        <el-table-column label="打卡规则" prop="punchCardRule" width="120">
          <template #default="{ row }">
            {{ getPunchCardRuleText(row.punchCardRule) }}
          </template>
        </el-table-column>
        <el-table-column label="活动状态" prop="status" width="100">
          <template #default="{ row }">
            <el-tag :type="row.status === 0 ? 'success' : 'danger'">
              {{ row.status === 0 ? '开启' : '关闭' }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

    <!-- 底部操作按钮 -->
    <template #footer>
      <el-button type="primary" @click="handleConfirm" v-if="multiple">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import * as RefundActivityApi from '@/api/mall/promotion/refund/refundActivity'
import { formatDate } from '@/utils/formatTime'

defineOptions({ name: 'RefundActivityTableSelect' })

defineProps({
  // 多选模式
  multiple: {
    type: Boolean,
    default: false
  }
})

// 列表的总页数
const total = ref(0)
// 列表的数据
const list = ref<RefundActivityApi.RefundActivityRespVO[]>([])
// 列表的加载中
const loading = ref(false)
// 弹窗的是否展示
const dialogVisible = ref(false)
// 查询参数
const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  name: '',
  status: undefined
})

// 选中的活动
const selectedActivity = ref<RefundActivityApi.RefundActivityRespVO>()
const selectedActivities = ref<RefundActivityApi.RefundActivityRespVO[]>([])

/** 打开弹窗 */
const open = (activity?: RefundActivityApi.RefundActivityRespVO | RefundActivityApi.RefundActivityRespVO[]) => {
  // 重置
  selectedActivity.value = undefined
  selectedActivities.value = []

  // 处理已选中
  if (activity) {
    if (Array.isArray(activity)) {
      selectedActivities.value = [...activity]
    } else {
      selectedActivity.value = activity
    }
  }

  dialogVisible.value = true
  resetQuery()
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await RefundActivityApi.getRefundActivityPage(queryParams.value)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.value = {
    pageNo: 1,
    pageSize: 10,
    name: '',
    status: undefined
  }
  getList()
}

/** 单选中时触发 */
const handleSingleSelected = (activity: RefundActivityApi.RefundActivityRespVO) => {
  selectedActivity.value = activity
  emit('change', activity)
  // 关闭弹窗
  dialogVisible.value = false
}

/** 多选变化时触发 */
const handleSelectionChange = (activities: RefundActivityApi.RefundActivityRespVO[]) => {
  selectedActivities.value = activities
}

/** 多选确认 */
const handleConfirm = () => {
  emit('change', selectedActivities.value)
  dialogVisible.value = false
}

/** 格式化时间戳 */
const formatTimestamp = (timestamp: number | string) => {
  if (!timestamp) return ''
  if (typeof timestamp === 'number') {
    return formatDate(new Date(timestamp))
  }
  return formatDate(new Date(parseInt(timestamp)))
}

/** 获取打卡规则文本 */
const getPunchCardRuleText = (rule: number) => {
  switch (rule) {
    case 1:
      return '持续打卡'
    case 2:
      return '非持续打卡'
    case 3:
      return '无需打卡'
    default:
      return '未知'
  }
}

// 提供 open 方法，用于打开弹窗
defineExpose({ open })

// 确认选择时的触发事件
const emit = defineEmits(['change'])
</script> 